<template>
  <div class="app-container">
    <!-- search -->
    <el-form
      ref="queryForm"
      :inline="true"
      :model="queryParams"
      label-suffix=":"
    >
      <el-form-item label="用户名">
        <el-input
          v-model="queryParams.username"
          placeholder="用户名"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户编号">
        <el-input
          v-model="queryParams.id"
          placeholder="用户编号"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleQuery">搜索</el-button>
        <el-button type="primary" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!-- btn -->
    <el-row :gutter="10" class="mb10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="handleAdd"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          :disabled="multiple"
        >删除
        </el-button>
      </el-col>
    </el-row>
    <!-- table -->
    <el-table
      v-loading="loading"
      :data="tableData"
      border
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户编号" prop="id" align="center" />
      <el-table-column label="用户名称" prop="username" align="center" />
      <el-table-column label="用户角色" prop="role" align="center" />
      <el-table-column label="创建时间" prop="createTime" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          >修改
          </el-button>
          <el-button
            type="text"
            icon="el-icon-delete"
            style="color:red"
            @click="handleDel(scope.row)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageSize"
      :limit.sync="queryParams.pageNum"
      @pagination="getList"
    />
    <!-- dialog -->
    <el-dialog
      :title="dialogTile"
      :visible.sync="open"
      width="30%"
      :close-on-click-modal="false"
    >
      <el-form
        ref="form"
        :model="form"
        label-suffix=":"
        label-width="100px"
        :rules="rules"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="form.username"
            placeholder="用户名"
          />
        </el-form-item>
        <el-form-item label="用户角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择用户角色" style="width:100%">
            <el-option
              v-for="item in roleOption"
              :key="item.id"
              :label="item.name"
              :value="item.name"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserList, userDel, userAdd } from '@/api/system/user'
export default {
  name: 'User',
  data() {
    return {
      queryParams: {
        pageSize: 1,
        pageNum: 10,
        username: undefined,
        id: undefined
      },
      open: false,
      dialogTile: '新增用户',
      total: 0,
      multiple: true,
      loading: false,
      form: {},
      ids: [],
      tableData: [],
      roleOption: [{
        id: 1,
        name: 'admin'
      }, {
        id: 2,
        name: 'editor'
      }],
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        role: [{ required: true, message: '角色不能为空', trigger: 'change' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      this.loading = true
      const { data } = await getUserList(this.queryParams)
      this.total = data.total
      this.tableData = data.items
      this.loading = false
    },
    handleQuery() {
      this.queryParams.pageSize = 1
      this.getList()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    resetQuery() {
      this.queryParams = {
        pageSize: 1,
        pageNum: 10,
        username: undefined,
        id: undefined
      }
      this.getList()
    },
    handleAdd() {
      this.open = true
      this.dialogTile = '新增用户'
    },
    handleEdit(row) {
      this.open = true
      this.dialogTile = '修改用户'
      this.form = row
    },
    close() {
      this.form = {}
      this.open = false
    },
    submit() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          const res = await userAdd(this.form)
          if (res.code === 20000) {
            this.msgSuccess(res.data.msg)
            this.open = false
            this.getList()
          } else {
            this.msgInfo('添加失败')
          }
          this.form = {}
        } else {
          return false
        }
      })
    },
    handleDel(row) {
      const ids = row.id || this.ids
      this.$confirm('是否确认删除该数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return userDel(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
